﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_purview_edit.aspx.cs" Inherits="Ccit.SysPlat.Purview.sys_purview_edit" %>
<%@ Register TagPrefix="ccit" Assembly="Ccit.ServerControls.ActionForm" Namespace="Ccit.ServerControls" %>
<html>
<head runat="server">
    <title></title>
    <script src="../script/jquery.js" type="text/javascript"></script> 
    <script src="../script/StringClass_Ext.js" type="text/javascript"></script>  
    <script src="../script/jwindow.js" type="text/javascript"></script> 
    <script src="../script/jsonlib.js" type="text/javascript"></script> 
    <script type="text/javascript">
    <!--
        function doCheck(vCheckType) {
            switch (vCheckType) {
                case "ROLENAME":  //角色名称
                    var ctrlRoleName = $get("txtRoleName");
                    var roleName = jQuery.trim(ctrlRoleName.value);
                    var roleId = $get("_hidRoleId").value;
                    var organId = $get("_hidOrganId").value;
                    var userType = $get("_hidUserType").value;                    
                    if (roleName != "") {
                        PageMethods.CheckRoleNameExist(roleName, roleId, organId, userType, ClientCallBack);
                    }
                    break;
            }
        }

        function doCheckChange(vChkId) {
            var ocurCheck = $get(vChkId);
            var ctrlCheckArray = document.getElementsByTagName("input");
            var iLen = ctrlCheckArray.length;
            var ocheck;
            var vCtrlId;
            var iIndex;
            var vfindId = vChkId + "_";
            /* 设置所有下级选中状态 */
            for (var i = 0; i < iLen; i++) {
                ocheck = ctrlCheckArray[i];
                if (ocheck.type == "checkbox") {
                    vCtrlId = ocheck.id;

                    iIndex = vCtrlId.indexOf(vfindId);
                    if (iIndex != -1) {
                        ocheck.checked = ocurCheck.checked;
                    }
                }
            }

        }

        function ClientCallBack(vResult) {
            if (null == vResult || vResult == "undefined") {
                alert("网络传输错误,没有正确的返回值,请检查网络是否畅通!");
                return;
            }

            eval("var resultObj=" + vResult);
            var operType = resultObj.OPERTYPE;
            switch (operType) {
                case "NAMECHECK":                
                    if (parseInt(resultObj.RSTSIGN) < 0) {
                        alert(resultObj.RSTMSG);
                        var ctrlRoleName = $get("txtRoleName");
                        ctrlRoleName.focus();
                    }
                    break;
                case "ROLESAVE":                     
                    alert(resultObj.RSTMSG);          
                    if (parseInt(resultObj.RSTSIGN) > 0) {
                        var ctrlType = resultObj.CTRLTYPE;
                        switch (ctrlType) {
                            case "1":
                                $get("_hidRoleId").value = resultObj.RSTSIGN;
                                var ctrlDel = $get("btnDel");
                                ctrlDel.style.visibility = "visible";
                                break;
                            case "2":
                                initAddNew();
                                break;
                        }
                        var vOrganId = $get("_hidOrganId").value;
                        var vPageParam = "organId=" + vOrganId;
                        var encodeParam = base64encode(vPageParam);
                        parent.frames[0].document.location.href = "./sys_purview_tree.aspx?" + encodeParam;
                    }                    
                    break;
                case "ROLEDELETE":
                    alert(resultObj.RSTMSG); 
                     if (parseInt(resultObj.RSTSIGN) > 0) {                        
                        var vOrganId = $get("_hidOrganId").value;
                        var vPageParam = "organId=" + vOrganId
                        var encodeParam = base64encode(vPageParam);
                        parent.frames[0].document.location.href = "./sys_purview_tree.aspx?" + encodeParam;
                        initAddNew();
                    }                   
                    break;
            }
            resultObj = null;
        }

        function doSaveCheck() {
            var ctrlRoleName = $get("txtRoleName");
            var vRoleName = ctrlRoleName.value;
            if (jQuery.trim(vRoleName) == "") {
                alert("角色名称不能为空!");
                ctrlRoleName.focus();
                return false;
            }

            var vUserRangeId = $get("_hidRoleRange").value;
            if (vUserRangeId == "") {
                alert("角色范围不能为空!");
                return false;
            }

            return true;

        }

        function doSave(ctrlType) {
            if (!doSaveCheck()) return;  //页面校验
            saveData(ctrlType);
        }

        function saveData(ctrlType) {
            var roleId = $get("_hidRoleId").value;
            var userType = $get("_hidUserType").value;
            var roleType = (userType == "0") ? "0" : "1";  //角色类型与当前登陆类型的枚举值是对应的

            var msRole = {};
            msRole.roleId = roleId;
            msRole.roleName = jQuery.trim($get("txtRoleName").value);
            msRole.roleDesc = "";
            msRole.rangeType = $get("_hidRangeType").value;
            msRole.rangeIds = $get("_hidRoleRange").value;
            msRole.sysRole = roleType;
            getRolePrvSet(msRole);

            var jsonExpr = JSON.stringify(msRole, function(key, value) { return value; });
            PageMethods.SaveRole(jsonExpr, ctrlType, ClientCallBack);

            msRole = null;        
        }

        function getRolePrvSet(msRole) {
            var otable = $get("tbPurview");
            var vRowLen = otable.rows.length;
            var orow;
            var ocheck;
            var vModMenuIds = "";           
            var vParam;
            var vParamArray;
            var vLeaf;
            var vMenuId;
            var vDataRangSign;
            var vDataRangeType;
            var vCustType;
            var vCustRangeId;
            var vCustRangeName;
            var radioArray;
            var vRadioName;
          
            var prvArray = [];
            for (var i = 0; i < vRowLen; i++) {
                orow = otable.rows(i);
                ocheck = orow.cells(0).children(2);
                if (!ocheck.checked)
                    continue;

                vParam = orow.cells(0).children(1).value;
                vParamArray = vParam.split("_");
                vLeaf = vParamArray[0];
                vMenuId = vParamArray[1];
                vDataRangSign = vParamArray[2];
                switch (vLeaf) {
                    case "0": //非叶子                        
                        break;
                    case "1": //叶子
                        var msPurview = {};
                        msPurview.menuId = vParamArray[1];
                        msPurview.isRangeSet = vDataRangSign;
                        if (vDataRangSign == "0") {//不需要设置范围
                            vDataRangeType = "";
                            vCustType = "0";
                            vCustRangeId = "";
                            vCustRangeName = "";
                        }
                        else {
                            vRadioName = orow.cells(0).children(4).name;
                            radioArray = document.getElementsByName(vRadioName);
                            for (var ii = 0; ii < radioArray.length; ii++) {
                                if (radioArray(ii).checked) {
                                    vDataRangeType = radioArray(ii).value;
                                    break;
                                }
                            }
                            if (vDataRangeType != "5") {
                                vCustType = "0";
                                vCustRangeId = "";
                                vCustRangeName = "";
                            }
                            else {
                                vCustType = orow.cells(0).children(16).value;
                                vCustRangeId = orow.cells(0).children(17).value;
                                vCustRangeName = orow.cells(0).children(18).value;
                            }
                        }
                        msPurview.rangeSign = vDataRangeType;
                        msPurview.custRangeType = vCustType;
                        msPurview.custRangeIds = vCustRangeId;
                        prvArray.push(msPurview);
                        break;
                    case "2":
                        vModMenuIds += (vModMenuIds == "") ? vParamArray[1] : "," + vParamArray[1];
                        break;
                }
            }
            
            msRole.modPrvIds = vModMenuIds;
            msRole.lstMenuPrv = prvArray;
        }

        function doDelete() {
            if (!confirm("删除当前角色,是否继续?"))
                return;

            var roleId = $get("_hidRoleId").value;            
            PageMethods.DeleteRole(roleId, ClientCallBack);
        }

        function initAddNew() {
            $get("_hidRoleId").value = "";            
            $get("_hidRangeType").value = "";
            $get("_hidRoleRange").value = "";
            $get("txtRoleRange").value = "";
            __doPostBack("lnbCallBack", "");
        }

        function doSetCustRange(vCtrlType, vCtrlId, vCtrlName, rdName) {
            var vDataRangeType = "";
            var radioArray = document.getElementsByName(rdName);
            for (var ii = 0; ii < radioArray.length; ii++) {
                if (radioArray(ii).checked) {
                    vDataRangeType = radioArray(ii).value;
                    break;
                }
            }

            if (vDataRangeType != "5") {
                if (confirm("确定要进行自定义范围设置?")) {
                    document.getElementById(rdName + "_5").checked = true;
                    OpenRangeSelect(vCtrlType, vCtrlId, vCtrlName, "0");
                }
            }
            else {
                OpenRangeSelect(vCtrlType, vCtrlId, vCtrlName, "0");
            }

        }

        function doSetRange(vCtrlType, vCtrlId, vCtrlName) {
            OpenRangeSelect(vCtrlType, vCtrlId, vCtrlName, "1");
        }


        function doSetRadio(rdName, suffixId) {
            var vDataRangeType = "";
            var radioArray = document.getElementsByName(rdName);
            for (var ii = 0; ii < radioArray.length; ii++) {
                if (radioArray(ii).checked) {
                    vDataRangeType = radioArray(ii).value;
                    break;
                }
            }

            if (vDataRangeType != "5") {
                document.getElementById("hid_oper_range_type" + suffixId + "_5").value = "";
                document.getElementById("hid_oper_range_id" + suffixId + "_5").value = "";
                document.getElementById("txt_oper_range_name" + suffixId + "_5").value = "";
            }
        }
          
       
    //-->
    </script>
</head>
<body>
    <ccit:ActionForm id="form1" runat="server">
    <input type="hidden" id="_hidRoleId" runat="server" />
    <input type="hidden" id="_hidUserType" runat="server" />
    <input type="hidden" id="_hidOrganId" runat="server" />    
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>
     <table class="tableborder" cellpadding="0" cellspacing="0" style="width:98%" align="center">
        <tr>
            <td style="width:10%" class="pformleft" align="right"><nobr>角色名称</nobr></td>
            <td style="width:90%" class="pformright">
                <asp:TextBox ID="txtRoleName" CssClass="input" onblur="doCheck('ROLENAME')" Width="98%" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width:10%" class="pformleft" align="right" valign="top">                
                角色范围<br />
                <a id='aSelRange' runat="server" href="javascript:doSetRange('_hidRangeType','_hidRoleRange','txtRoleRange')">
                    <img src="../images/toolbar/user.gif" border="0" />
                </a>
                
            </td>
            <td style="width:90%" class="pformright">
                <input type="hidden" id="_hidRangeType" runat="server" />
                <input type="hidden" id="_hidRoleRange" runat="server" />
                <asp:TextBox ID="txtRoleRange" TextMode="MultiLine" runat="server" Width="98%" rows="6"></asp:TextBox>
            </td>            
        </tr>
        <tr>
            <td colspan="2" class="tbToolbar" style="width:100%">&nbsp;<strong>权限设置</strong></td>
        </tr>
        <tr>
            <td style="width:100%" colspan="2">
                <table id="tbPurview" cellpadding="2" cellspacing="2" runat="server" style="width:100%">                    
                </table>
            </td>
        </tr>
        <tr class="pformstrip">
            <td colspan="2" width="100%" align="center">
                <input type="button" id="btnSave" class="button" value="保存" onclick="doSave('1')" />
                &nbsp;
                <input type="button" id="btnSaveAdd" class="button" value="保存并新增" onclick="doSave('2')" />
                &nbsp;
                <input type="button" id="btnDel" class="button" value="删除" onclick="doDelete()" />
            </td>
        </tr>
    </table> 
    <asp:LinkButton ID="lnbCallBack" runat="server" style="display:none" onclick="lnbCallBack_Click"></asp:LinkButton>   
    </ccit:ActionForm>
    <script type="text/javascript">
    <!--
        $(document).ready(function() {
            var roleId = $get("_hidRoleId").value;
            var ctrlDel = $get("btnDel");

            ctrlDel.style.visibility = (roleId == "") ? "hidden" : "visible";            
        });
    //-->
    </script>
</body>
</html>
